<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
// import echarts from 'echarts'
import * as echarts from 'echarts';
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'

// const animationDuration = 6000

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '645px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      var salvProName = ['编内', '备案制', '合同制', '委派', '返聘', '外聘', '试用期', '见习期', '见习基地', '劳务派遣', '试工']
      var salvProValue = [179, 52, 200, 334, 390, 330, 220, 334, 390, 330, 220]
      this.chart.setOption({
        title: {
          text: '人员身份统计',
          left: 'left',
          top: '0%',
          textStyle: {
            color: '#000', // 标题颜色
            fontWeight: 'bold',
            fontSize: 16
          }
        },
        backgroundColor: '#fff',
        grid: {
          left: '2%',
          right: '2%',
          bottom: '2%',
          top: 60,
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          formatter: '{b}:{c}'
        },
        xAxis: {
          show: false,
          type: 'value'
        },
        yAxis: [{
          type: 'category',
          inverse: true,
          axisLabel: {
            show: false,
            textStyle: {
              color: 'rgba(0, 0, 0, 0.40)'
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          data: salvProName
        }],
        series: [
          {
            name: 'value',
            type: 'bar',
            zlevel: 1,
            showBackground: true,
            backgroundStyle: {
              color: '#f5f8ff'
            },
            itemStyle: {
              normal: {
                barBorderRadius: [0, 20, 20, 0], // 圆角（左上、右上、右下、左下）
                color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                  '#2FAEF2', '#1CD8A8'
                ].map((color, offset) => ({
                  color,
                  offset
                }))) // 渐变
              }
            },
            label: {
              normal: {
                color: 'rgba(0, 0, 0, 0.40)',
                show: true,
                position: [0, '-20px'],
                textStyle: {
                  fontSize: 16
                },
                formatter: '{b}'
              }
            },
            barWidth: 20,
            data: salvProValue
          },
          {
            name: '21',
            type: 'bar',
            barWidth: 20,
            barGap: '-100%',
            data: salvProValue,
            itemStyle: {
              normal: {
                color: '#f5f8ff'
              },
              emphasis: {
                color: '#f5f8ff'
              }
            },
            label: {
              normal: {
                color: 'rgba(0, 0, 0, 0.30)',
                show: true,
                position: ['100%', '2px'], //
                textStyle: {
                  fontSize: 16
                }
              }
            }
          }
        ]
      })
    }
  }
}
</script>
